{$i-,s-,g+,r-}
{$ifndef modif}{$i modif.inc}{$endif}
Program AddPictures;
uses {$ifdef Windows}
     WinCrtx,WinDos,
     {$else}
      {$ifdef crtx}crtx,{$else}crt,{$endif}dos,
     {$endif}
     pictures,ToolsB;

var vystup,a:string;
    obr1,obr2:Picture;
    i,j:integer;
    MaxP,p:Byte;
    MarkedTime:Longint;
    n:real;
    PassLProc:PercentLineProc;


Function GetInterval:Longint; assembler;
asm
	MOV	AH,0
	INT	1Ah
	MOV	AX,DX
	MOV	DX,CX
end;


Label 1,10,20,Finish;


const Title:array[0..16]of char='Mask Filtration'#0;
begin
{$ifdef Windows}
ScreenSize.X:=62;
ScreenSize.Y:=10;
WindMax:=ScreenSize;
Move(Title,WindowTitle,Sizeof(Title));
{$endif}
PassLProc.init;
obr1.init;
obr2.init;
{$ifndef  Windows}
  DirectVideo:=False;
  nosound;
{$endif}
 writeln(#10#13'<<< ADD >>> Add images (c)2011 F&TSoft');
 vystup:='';
 n:=1;
 if Paramcount=0 then
	begin
1:	write('Enter name of 1st input picture:');readln(a);
	if a='' then goto 1;
	assignpicture(obr1,a);
	write('Enter name of 2nd input picture:');readln(a);
	if a='' then goto 1;
	assignpicture(obr2,a);
	write('Enter name of output picture:');readln(a);
	vystup:=a;
10:     write('Enter size of mask:');readln(a);
	val(a,n,j);
	if (j<>0)or(n<=0) then
		     begin
		     writeln('Error:Incorrect number ',a);
		     goto 10;
		     end;
	end;

 for i:=1 to paramcount do
	begin
	a:=paramStr(i);
	if((a[2]='?')or(a[1]='?'))or((UpCases(a)='/HELP')or(UpCases(a)='-HELP')) then
		begin
		writeln(#10#13'MASK file_in file_out [size] '#10#13+
			      'Supported file formats are BMP,OKO,PCX,RAS'#10#13);
		{$ifdef Windows}InactiveWinCrt;{$endif}
		exit;
		end;

	if a[1] in ['-','+','0'..'9'] then
		      begin
		      val(a,n,j);
		      if j<>0 then begin
				   writeln('Error:Incorrect number ',a);
				   {$ifdef Windows}InactiveWinCrt;{$endif}
				   exit;
				   end;
		      end
		else
		begin
		if obr1.filename=nil
		  then assignpicture(obr1,a)
		  else assignpicture(obr2,a);
		vystup:=a;
		end;

	end;

 if n<0.9 then
	begin
	writeln('Error:Incorrect size');
	{$ifdef Windows}InactiveWinCrt;{$endif}
	exit;
	end;

 if obr1.filename=nil then
	begin
	writeln('Error:Unknown filename');
	{$ifdef Windows}
	while PeepError<>0 do writeln('Error: ',GetErrorString(ReadError));
	InactiveWinCrt;
	exit;
	{$else}
	goto Finish;
	{$endif}
	end;
 if obr2.filename=nil then
	begin
	writeln('Error:Unknown filename');
	{$ifdef Windows}
	while PeepError<>0 do writeln('Error: ',GetErrorString(ReadError));
	InactiveWinCrt;
	exit;
	{$else}
	goto Finish;
	{$endif}
	end;
 write(#13'Loading ');ClrEol;
 loadpicture(obr1,'',@PassLProc);{} {  loadpicture(fbr);{}
 loadpicture(obr2,'',@PassLProc);{} {  loadpicture(fbr);{}
 if obr1.data=nil then
	begin
	writeln(#10#13'Cannot find input picture:''',obr1.filename^,'''');
	{$ifdef Windows}
	while PeepError<>0 do writeln('Error: ',GetErrorString(ReadError));
	InactiveWinCrt;
	exit;
	{$else}
	goto Finish;
	{$endif}
	end;
 if obr2.data=nil then
	begin
	writeln(#10#13'Cannot find input picture:''',obr2.filename^,'''');
	{$ifdef Windows}
	while PeepError<>0 do writeln('Error: ',GetErrorString(ReadError));
	InactiveWinCrt;
	exit;
	{$else}
	goto Finish;
	{$endif}
	end;


 markedTime:=GetInterval;
 write(#13'Filtering ');ClrEol;
 Operation2(Obr1,Obr2,Add,nil,@PassLProc);
 obr2.Done;

 write(#13'Saving  ');ClrEol;
 if not(savepicture(obr1,vystup,@PassLProc)) then
	begin
	writeln(#10#13'Cannot save Output picture:''',obr1.filename^,'''');
{$ifdef Windows}InactiveWinCrt;{$endif}
	exit;
	end;
 writeln(#13' OK     ');ClrEol;

Finish:
 while PeepError<>0 do writeln('Error: ',GetErrorString(ReadError));
 obr1.Done;
end.
